from unittest.mock import patch
from unittest import TestCase

import requests

from db.database import DatabaseManager
from tools.webhook import WebHook
from code_spectra.code_spectra import WebHookProcessor


STRENGTHEN_BRANCH = "OpenHarmony_feature_20250328"
MASTER_BRANCH = "master"

default_pr_body = ('1. fix\r\n---\r\n**IssueNo**:\r\n'
                   '**Description**: (提交描述)\r\n'
                   '**Sig**: SIG_ApplicationFramework\r\n'
                   '**Feature or Bugfix**: (需求填Feature,缺陷填Bugfix)\r\n'
                   '**Binary Source**: No(涉及则Yes)\r\n'
                   '**是否涉及非兼容变更**: 是\r\n'
                   '**TDD自验结果(如有失败用例需提供基线执行情况)**: Pass\r\n'
                   '**XTS自验结果(如有失败用例需提供基线执行情况)**: Pass\r\n'
                   '**检视意见是否都已闭环(无检视意见请填Yes)**: Yes')

def make_merge_hook(person, action="assign", branch="master"):
    hook = {
        "author": {
            "html_url": "https://gitee.com/royce-ls"
        },
        "action": action,
        "pull_request": {
            "number": "12345",
            "base": {
                "ref": branch,
                "repo": {
                    'id': 'arkui_ace_engine',
                    'owner': {
                        'id': 'openharmony',
                    }
                }
            },
            "body": default_pr_body
        },
        "hook_name": "merge_request_hooks",
        "target_user": None
    }
    if action == "assign":
        hook["target_user"] = {
            "html_url": person
        }
    if action in ["open", "update"]:
        hook["author"]["html_url"] = person
        if action == "update":
            hook["action_desc"] = "source_branch_changed"
    return WebHook(hook)


def make_note_hook(commenter, body, branch="master", pr_body=""):
    if not pr_body:
        pr_body = default_pr_body
    hook = {
        "author": {
            "html_url": "https://gitee.com/royce-ls"
        },
        "action": "comment",
        "pull_request": {
            "number": "12345",
            "base": {
                "ref": branch,
                "repo": {
                    'id': 'arkui_ace_engine',
                    'owner': {
                        'id': 'openharmony',
                    }
                }
            },
            "body": pr_body
        },
        "hook_name": "note_hooks",
        "comment": {
            'user': {
                'html_url': commenter,
            },
            'body': body,
        }
    }
    return WebHook(hook)


class TestWebHookProcessor(TestCase):
    comment = None
    comment_count = 0
    review = False
    db = DatabaseManager()

    def reset(self, reset_db=True):
        self.comment = None
        self.comment_count = 0
        self.review = 0
        if reset_db:
            self.db.clean_database()
            self.db.init_db()

    def my_comment(self, pr_number, headers, json):
        self.comment = json
        self.comment_count += 1
        return requests.Response()

    def my_review(self, pr_number, headers):
        self.review += 1
        return requests.Response()

    def test_pass_failed_when_tdd_not_pass(self):
        """
        TDD检查未通过不允许pass加分
        """
        self.reset()
        hook = make_note_hook("https://gitee.com/liyujie43", "pass")
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["a.h"]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertIn("加分失败", str(self.comment))
            self.assertIn('联系以下人员检视后评论"force tdd"', str(self.comment))
            self.assertEqual(self.review, 0)
            self.assertEqual(self.comment_count, 1)

    def test_pass_succeed_when_tdd_force_pass(self):
        """
        TDD检查强制通过后允许pass加分
        """
        self.reset()
        hook = make_note_hook("https://gitee.com/liyujie43", "pass")
        force_hook = make_note_hook("https://gitee.com/ileft2019", "force tdd")
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["a.h"]
            get_comments.return_value = []
            WebHookProcessor(True).process(force_hook)
            self.assertIn("TDD检查已强制通过", str(self.comment))
            self.assertEqual(self.comment_count, 1)
            self.reset(False)
            WebHookProcessor(True).process(hook)
            self.assertIn("已评审通过, 责任田", str(self.comment))
            self.assertEqual(self.review, 2)
            self.assertEqual(self.comment_count, 1)

    def test_pass_succeed_when_tdd_pass(self):
        """
        TDD检查通过允许pass加分
        """
        self.reset()
        hook = make_note_hook("https://gitee.com/liyujie43", "pass")
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["unittest/a.h"]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertIn("已评审通过, 责任田", str(self.comment))
            self.assertEqual(self.review, 2)
            self.assertEqual(self.comment_count, 1)

    def test_pass_succeed_when_tdd_in_white_list(self):
        """
        TDD检查通过（白名单场景）允许pass加分
        """
        self.reset()
        hook = make_note_hook("https://gitee.com/liyujie43", "pass")
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["frameworks/bridge/a.h"]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertIn("已评审通过, 责任田", str(self.comment))
            self.assertEqual(self.review, 2)
            self.assertEqual(self.comment_count, 1)
            self.reset()
            get_filenames.return_value = ["frameworks/core/components_ng/BUILD.gn"]
            WebHookProcessor(True).process(hook)
            self.assertIn("已评审通过, 责任田", str(self.comment))
            self.assertEqual(self.review, 2)
            self.assertEqual(self.comment_count, 1)

    def test_pass_failed_when_intrusive_not_pass(self):
        """
        侵入式修改检查未通过不允许pass加分
        """
        self.reset()
        hook = make_note_hook("https://gitee.com/liyujie43", "pass", branch=STRENGTHEN_BRANCH)
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["frameworks/core/components_ng/button_model_static.cpp"]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertIn("加分失败", str(self.comment))
            self.assertIn('force mod', str(self.comment))
            self.assertEqual(self.review, 0)
            self.assertEqual(self.comment_count, 1)

    def test_pass_succeed_when_intrusive_force_pass(self):
        """
        侵入式修改检查强制通过后允许pass加分
        """
        self.reset()
        hook = make_note_hook("https://gitee.com/liyujie43", "pass", branch=STRENGTHEN_BRANCH)
        force_hook = make_note_hook("https://gitee.com/sunfei2021", "force mod", branch=STRENGTHEN_BRANCH)
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["frameworks/core/components_ng/button_model_static.cpp"]
            get_comments.return_value = []
            WebHookProcessor(True).process(force_hook)
            self.assertIn("侵入式修改检查已强制通过", str(self.comment))
            self.assertEqual(self.comment_count, 1)
            self.reset(False)
            WebHookProcessor(True).process(hook)
            self.assertIn("已评审通过, 责任田", str(self.comment))
            self.assertEqual(self.review, 2)
            self.assertEqual(self.comment_count, 1)

    def test_pass_succeed_when_intrusive_pass(self):
        """
        侵入式修改检查通过允许pass加分
        """
        self.reset()
        hook = make_note_hook("https://gitee.com/liyujie43", "pass", branch=STRENGTHEN_BRANCH)
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["frameworks/core/components_ng/pattern/button/button_model_static.cpp"]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertIn("已评审通过, 责任田", str(self.comment))
            self.assertEqual(self.review, 2)
            self.assertEqual(self.comment_count, 1)

    def test_pass_succeed_when_intrusive_pass_in_whitelist(self):
        """
        侵入式修改检查通过（白名单场景）允许pass加分
        """
        self.reset()
        hook = make_note_hook("https://gitee.com/liyujie43", "pass", branch=STRENGTHEN_BRANCH)
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["frameworks/core/components_ng/BUILD.gn"]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertIn("已评审通过, 责任田", str(self.comment))
            self.assertEqual(self.review, 2)
            self.assertEqual(self.comment_count, 1)
            self.reset()
            get_filenames.return_value = ["test/core/components_ng/a.h"]
            WebHookProcessor(True).process(hook)
            self.assertIn("已评审通过, 责任田", str(self.comment))
            self.assertEqual(self.review, 2)
            self.assertEqual(self.comment_count, 1)

    def test_pass_all(self):
        """
        全体加分
        """
        self.reset()
        hook = make_note_hook("https://gitee.com/sunfei2021", "pass all")
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["frameworks/core/components_ng/BUILD.gn"]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertEqual(self.review, 18)

    def test_summon_dragon(self):
        """
        召唤神龙
        """
        self.reset()
        hook = make_note_hook("https://gitee.com/liyujie43", "summon dragon", branch="OpenHarmony_ArkUI_Upstream_2024")
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["frameworks/core/components_ng/pattern/button/button_model_static.cpp"]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertEqual(self.review, 0)
            self.assertEqual(self.comment_count, 1)

    def test_assign(self):
        """
        分配小组审查人时评论组员
        """
        self.reset()
        hook = make_merge_hook("https://gitee.com/arkui_image")
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["frameworks/core/components_ng/pattern/button/button_model_static.cpp"]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertIn("修改责任田", str(self.comment))
            self.assertIn("arkui_image", str(self.comment))
            self.assertEqual(self.review, 0)
            self.assertEqual(self.comment_count, 1)

    def test_assign_nothing(self):
        """
        分配一般审查人时无动作
        """
        self.reset()
        hook = make_merge_hook("https://gitee.com/royce-ls")
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["frameworks/core/components_ng/pattern/button/button_model_static.cpp"]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertIsNone(self.comment)
            self.assertEqual(self.review, 0)
            self.assertEqual(self.comment_count, 0)

    def test_check_tdd_when_open(self):
        """
        开启PR时检查tdd
        """
        self.reset()
        hook = make_merge_hook("https://gitee.com/royce-ls", action="open")
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["frameworks/core/components_ng/pattern/button/button_model_static.cpp"]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertIn("force tdd", str(self.comment))
            self.assertEqual(self.review, 0)
            self.assertEqual(self.comment_count, 1)

    def test_check_tdd_in_whitelist_when_open(self):
        """
        开启PR时检查tdd
        """
        self.reset()
        hook = make_merge_hook("https://gitee.com/royce-ls", action="open")
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["interfaces/napi/button_model_static.cpp"]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertNotIn("force tdd", str(self.comment))
            self.assertEqual(self.review, 0)
            self.assertEqual(self.comment_count, 1)

    def test_check_mod_when_open(self):
        """
        开启PR时检查侵入式修改
        """
        self.reset()
        hook = make_merge_hook("https://gitee.com/royce-ls", action="open", branch=STRENGTHEN_BRANCH)
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["frameworks/core/components_ng/button_model_static.cpp"]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertIn("force mod", str(self.comment))
            self.assertEqual(self.review, 0)
            self.assertEqual(self.comment_count, 1)

    def test_check_tdd_when_update(self):
        """
        更新PR时检查tdd
        """
        self.reset()
        hook = make_merge_hook("https://gitee.com/royce-ls", action="update")
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["frameworks/core/components_ng/pattern/button/button_model_static.cpp"]
            WebHookProcessor(True).process(hook)
            self.assertIn("请自行检视是否需要补充用例", str(self.comment))
            self.assertEqual(self.review, 0)

    def test_check_mod_when_update(self):
        """
        更新PR时检查侵入式修改
        """
        self.reset()
        hook = make_merge_hook("https://gitee.com/royce-ls", action="update", branch=STRENGTHEN_BRANCH)
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["frameworks/core/components_ng/button_model_static.cpp"]
            WebHookProcessor(True).process(hook)
            self.assertIn("force mod", str(self.comment))
            self.assertEqual(self.review, 0)

    def test_check_comments_when_open(self):
        """
        开启PR时检查tdd
        """
        self.reset()
        hook = make_merge_hook("https://gitee.com/royce-ls", action="open")
        with patch('tools.git_client.GitClient.get_files') as get_files, \
                patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["interfaces/napi/button_model_static.cpp"]
            get_files.return_value = [{"filename": "interfaces/napi/button_model_static.cpp", "additions": 1000}]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertIn("本检查项默认不自动执行", str(self.comment))
            self.assertEqual(self.review, 0)
            self.assertEqual(self.comment_count, 1)

    def test_pass_succeed_when_comments_force_pass(self):
        """
        侵入式修改检查强制通过后允许pass加分
        """
        self.reset()
        hook = make_note_hook("https://gitee.com/liyujie43", "pass")
        force_hook = make_note_hook("https://gitee.com/liyujie43", "force comments")
        with patch('tools.git_client.GitClient.get_files') as get_files, \
                patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["interfaces/napi/button_model_static.cpp"]
            get_files.return_value = [{"filename": "interfaces/napi/button_model_static.cpp", "additions": 1000}]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertIn("检视意见数量未达标", str(self.comment))
            self.assertEqual(self.comment_count, 1)
            self.reset(False)
            WebHookProcessor(True).process(force_hook)
            self.reset(False)
            WebHookProcessor(True).process(hook)
            self.assertIn("已评审通过, 责任田", str(self.comment))
            self.assertEqual(self.review, 2)
            self.assertEqual(self.comment_count, 1)

    def test_pass_failed_when_check_comment_count_failed(self):
        """
        检视意见数检查未通过不允许pass加分
        """
        self.reset()
        hook = make_note_hook("https://gitee.com/liyujie43", "pass", branch=MASTER_BRANCH)
        with patch('tools.git_client.GitClient.get_files') as get_files, \
                patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = ["frameworks/core/components_ng/BUILD.gn"]
            get_files.return_value = [{"filename": "frameworks/core/components_ng/BUILD.gn", "additions": 1000}]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertIn("加分失败", str(self.comment))
            self.assertIn("检视意见数量未达标", str(self.comment))
            self.assertEqual(self.review, 0)
            self.assertEqual(self.comment_count, 1)

    def test_pass_success_when_check_comment_count_is_enough(self):
        """
        检视意见数检查通过允许pass加分
        """
        self.reset()
        hook = make_note_hook("https://gitee.com/liyujie43", "pass", branch=MASTER_BRANCH)
        with patch('tools.git_client.GitClient.get_files') as get_files, \
                patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = [
                "frameworks/core/components_ng/BUILD.gn",
                "test/unittest/core/pattern/picker/date_picker_test_two.cpp"
            ]
            get_files.return_value = [
                {"filename": "frameworks/core/components_ng/BUILD.gn", "additions": 100},
                {"filename": "test/unittest/core/pattern/picker/date_picker_test_two.cpp", "additions": 100},
            ]
            get_comments.return_value = [
                {"path": "frameworks/core/components_ng/BUILD.gn"},
                {"path": "frameworks/core/components_ng/BUILD.gn"},
                {"path": "test/unittest/core/pattern/picker/date_picker_test_two.cpp"},
                {"path": "test/unittest/core/pattern/picker/date_picker_test_two.cpp"},
            ]
            WebHookProcessor(True).process(hook)
            self.assertIn("已评审通过", str(self.comment))
            self.assertEqual(self.review, 2)
            self.assertEqual(self.comment_count, 1)

    def test_pass_success_when_check_addition_count_less_than_threshold(self):
        """
        小于阈值但是需要有检视意见数，检查通过允许pass加分
        """
        self.reset()
        hook = make_note_hook("https://gitee.com/liyujie43", "pass", branch=MASTER_BRANCH)
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.get_files') as get_files, \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = [
                "frameworks/core/components_ng/BUILD.gn",
                "test/unittest/core/pattern/picker/date_picker_test_two.cpp"
            ]
            get_files.return_value = [
                {"filename": "frameworks/core/components_ng/BUILD.gn", "additions": 50},
                {"filename": "test/unittest/core/pattern/picker/date_picker_test_two.cpp", "additions": 50},
            ]
            get_comments.return_value = [
                {"path": "frameworks/core/components_ng/BUILD.gn"},
                {"path": "test/unittest/core/pattern/picker/date_picker_test_two.cpp"},
            ]
            WebHookProcessor(True).process(hook)
            self.assertIn("已评审通过", str(self.comment))
            self.assertEqual(self.review, 2)
            self.assertEqual(self.comment_count, 1)

    def test_check_commit_msg_success_when_pass(self):
        """
        pass时检查提交信息通过
        """
        self.reset()
        pr_body = ('1. fix\r\n---\r\n**IssueNo**:\r\n'
                   '**Description**: (提交描述)\r\n'
                   '**Sig**: SIG_ApplicationFramework\r\n'
                   '**Feature or Bugfix**: (需求填Feature,缺陷填Bugfix)\r\n'
                   '**Binary Source**: No(涉及则Yes)\r\n'
                   '**是否涉及非兼容变更**: 是\r\n'
                   '**TDD自验结果(如有失败用例需提供基线执行情况)**: Pass\r\n'
                   '**XTS自验结果(如有失败用例需提供基线执行情况)**: Pass\r\n'
                   '**检视意见是否都已闭环(无检视意见请填Yes)**: Yes')
        hook = make_note_hook("https://gitee.com/liyujie43", "pass", branch=MASTER_BRANCH, pr_body=pr_body)
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.get_files') as get_files, \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = [
                "test/unittest/core/pattern/picker/date_picker_test_two.cpp"
            ]
            get_files.return_value = [
                {"filename": "test/unittest/core/pattern/picker/date_picker_test_two.cpp", "additions": 49},
            ]
            get_comments.return_value = [{}]
            WebHookProcessor(True).process(hook)
            self.assertIn("已评审通过", str(self.comment))
            self.assertEqual(self.review, 2)
            self.assertEqual(self.comment_count, 1)

    def test_check_commit_msg_fail_when_pass1(self):
        """
        pass时检查提交信息
        """
        self.reset()
        pr_body = ('1. fix\r\n---\r\n**IssueNo**:\r\n'
                   '**Description**: (提交描述)\r\n'
                   '**Sig**: SIG_ApplicationFramework\r\n'
                   '**Feature or Bugfix**: (需求填Feature,缺陷填Bugfix)\r\n'
                   '**Binary Source**: No(涉及则Yes)\r\n'
                   '**是否涉及非兼容变更**: (是/否/不涉及)\r\n'
                   '**TDD自验结果(如有失败用例需提供基线执行情况)**: (Pass/Fail/不涉及)\r\n'
                   '**XTS自验结果(如有失败用例需提供基线执行情况)**: Pass\r\n'
                   '**检视意见是否都已闭环(无检视意见请填Yes)**: Yes')
        hook = make_note_hook("https://gitee.com/liyujie43", "pass", branch=MASTER_BRANCH, pr_body=pr_body)
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.get_files') as get_files, \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = [
                "test/unittest/core/pattern/picker/date_picker_test_two.cpp"
            ]
            get_files.return_value = [
                {"filename": "test/unittest/core/pattern/picker/date_picker_test_two.cpp", "additions": 120},
            ]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertIn("加分失败", str(self.comment))
            self.assertEqual(self.review, 0)
            self.assertEqual(self.comment_count, 1)

    def test_check_commit_msg_fail_when_pass2(self):
        """
        pass时检查提交信息不规范
        """
        self.reset()
        pr_body = ('1. fix\r\n---\r\n**IssueNo**:\r\n'
                   '**Description**: 蓝区强基单框代码修改，黄区问题单DTS9876543210987，不涉及双框架，修改人：a123456\r\n'
                   '经评审已转需求AR98765432101234\r\n'
                   '**Sig**: SIG_ApplicationFramework\r\n'
                   '**Feature or Bugfix**: (需求填Feature,缺陷填Bugfix)\r\n'
                   '**Binary Source**: No(涉及则Yes)\r\n'
                   '**是否涉及非兼容变更**: 是\r\n'
                   '**TDD自验结果(如有失败用例需提供基线执行情况)**: Pass\r\n'
                   '**XTS自验结果(如有失败用例需提供基线执行情况)**: Pass\r\n'
                   '**检视意见是否都已闭环(无检视意见请填Yes)**: Yes')
        hook = make_note_hook("https://gitee.com/liyujie43", "pass", branch=MASTER_BRANCH, pr_body=pr_body)
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.get_files') as get_files, \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = [
                "test/unittest/core/pattern/picker/date_picker_test_two.cpp"
            ]
            get_files.return_value = [
                {"filename": "test/unittest/core/pattern/picker/date_picker_test_two.cpp", "additions": 120},
            ]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertIn("请注意检查提交信息中的规范用语", str(self.comment))
            self.assertEqual(self.review, 0)
            self.assertEqual(self.comment_count, 1)

    def test_list_commands(self):
        """
        查询命令列表
        """
        self.reset()
        hook = make_note_hook("https://gitee.com/liyujie43", "list commands", branch=MASTER_BRANCH)
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.get_files') as get_files, \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = [
                "test/unittest/core/pattern/picker/date_picker_test_two.cpp"
            ]
            get_files.return_value = [
                {"filename": "test/unittest/core/pattern/picker/date_picker_test_two.cpp", "additions": 120},
            ]
            get_comments.return_value = []
            WebHookProcessor(True).process(hook)
            self.assertNotIn("您可通过评论", str(self.comment))
            self.assertIn("命令", str(self.comment))
            self.assertEqual(self.review, 0)
            self.assertEqual(self.comment_count, 1)

    def test_list_commands_after_comment(self):
        """
        评论后默认跟一个帮助信息
        """
        self.reset()
        hook = make_note_hook("https://gitee.com/liyujie43", "pass", branch=MASTER_BRANCH)
        with patch('tools.git_client.GitClient.get_filenames') as get_filenames, \
                patch('tools.git_client.GitClient.get_files') as get_files, \
                patch('tools.git_client.GitClient.get_comments') as get_comments, \
                patch('tools.git_client.GitClient.review', side_effect=self.my_review), \
                patch('tools.git_client.GitClient.comment', side_effect=self.my_comment):
            get_filenames.return_value = [
                "test/unittest/core/pattern/picker/date_picker_test_two.cpp"
            ]
            get_files.return_value = [
                {"filename": "test/unittest/core/pattern/picker/date_picker_test_two.cpp", "additions": 50},
            ]
            get_comments.return_value = [{}, {}]
            WebHookProcessor(True).process(hook)
            self.assertIn("您可通过评论", str(self.comment))
            self.assertEqual(self.review, 2)
            self.assertEqual(self.comment_count, 1)
    
    def test_check_multi_thread(self):
        """
        开启PR是时执行多线程映射检查
        """
        hook = make_merge_hook("https://gitcode.com/royce-ls", action="open")
        
        

